package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.client.MemberClient;
import cn.com.connext.scrm.domain.master.Activity;
import cn.com.connext.scrm.domain.master.AwardRecord;
import cn.com.connext.scrm.domain.master.NewDrawActivity;
import cn.com.connext.scrm.service.ActivityService;
import cn.com.connext.scrm.service.NewDrawActivityService;
import cn.com.connext.scrm.service.dto.*;
import cn.com.connext.scrm.service.mapper.ActivitiesService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.*;
import cn.com.connext.scrm.web.rest.resp.QueryActivitiesResp;
import cn.com.connext.scrm.web.rest.resp.QueryNowAndPostActivitiesResp;
import cn.com.connext.scrm.web.rest.vm.*;
import com.alibaba.fastjson.JSON;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.exception.ServiceException;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Created by WangZhan on 2019/01/24.
 */
@RestController
@RequestMapping("/api/newdrawactivity")
public class NewDrawActivityResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(NewDrawActivityResource.class);

    @Autowired
    private NewDrawActivityService newDrawActivityService;

    public NewDrawActivityResource() {
        super("NewDrawActivity");
    }

    /**
     * 分页查询新活动附件列表
     */
    @ApiOperation(value = "分页查询新活动附件列表", notes = "分页查询新活动附件列表")
    @PostMapping(value = "/findByPage")
    public JsonResult findByPage(@RequestBody NewDrawActivityVM ndav ){
        JsonResult result = new JsonResult();
        try{
            Page<NewDrawActivity> page = newDrawActivityService.findByPage(ndav);
            result.setData(page);
            result.setState(0);
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 新增核销码
     */
    @ApiOperation(value = "新增新活动", notes = "新增新活动")
    @PostMapping(value = "/add")
    public JsonResult add(@RequestBody NewDrawActivity entity ){
        log.info("参数: {}",entity.toString());
        return  newDrawActivityService.save(entity);
    }


    /**
     * 新增核销码
     */
    @ApiOperation(value = "删除活动子模块", notes = "删除活动子模块")
    @PostMapping(value = "/delete")
    public JsonResult delete(@RequestBody NewDrawActivity entity ){
        return  newDrawActivityService.delete(entity);
    }

    /**
     * 根据path查询信息
     */
    @ApiOperation(value = "根据path查询信息", notes = "根据path查询信息")
    @PostMapping(value = "/findByPath")
    public JsonResult findByPath(@RequestBody NewDrawActivity entity ){
        log.info("参数: {}",entity.toString());
        return  newDrawActivityService.findByPath(entity);
    }

    /**
     * 0810查询新活动附件列表
     */
    @ApiOperation(value = "查询新活动附件列表", notes = "查询新活动附件列表")
    @PostMapping(value = "/findByDrawActivityId")
    public JsonResult findByDrawActivityId(@RequestBody NewDrawActivity entity){
        JsonResult result = new JsonResult();
        try{
            List<NewDrawActivity> ndav = newDrawActivityService.findByDrawActivityId(entity.getDrawActivityId());
            result.setData(ndav);
            result.setState(0);
            result.setMessage("查询成功");
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 0810新增新活动
     */
    @ApiOperation(value = "新增新活动", notes = "新增新活动")
    @PostMapping(value = "/newAdd")
    public JsonResult newAdd(@RequestBody NewDrawActivityAddVM entity){
        JsonResult result = new JsonResult();
        try{
            if (entity.getNewDrawActivityList() != null && entity.getNewDrawActivityList().size() > 0){
            newDrawActivityService.newAdd(entity);
            result.setState(0);
            result.setMessage("增加成功");
            }
            else {
               result.setState(-1);
               result.setMessage("增加失败");
            }

        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("增加失败");
        }
        return result;
    }
}
